<%@ page contentType="text/html; charset=utf-8"%>
<%@ include file="/global/jsp-header.jsp"%>
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html">
		<title>权限设置</title>
		<%@ include file="/global/tablesort-header.jsp"%>
		<style type="text/css">
			.childMenu {text-indent: 25px;}
			#operatorSuccess{display:none;color:red;text-align: inherit;}
		</style>
		<%
			/*
			权限设置，页面checkbox控制思路:主要在level上面，顶级目录以“m_${menuId}”开始，此后的分别加上当前的类型，并以"_"分割
			比如:父菜单${m_pm1},子菜单${m_pm1_m1},操作${m_pm1_m1_a1}数据范围${m_pm1_m1_a1_r1}
			当选择子checkbox时，采用递归的方式，递归到最根的并选中,实现方式也是根据level来判断，由于有上面的规则，所以递归退出的规则为split("_")后只存在2级，则为根菜单
			*/
		%>
		<script type="text/javascript">
	        $(function () {
	        	$('#myTable').tablesorter({ headers: { 0: { sorter: false}, 1: {sorter: false},2: {sorter: false} } });
	        	
	        	$("input:checkbox,input:radio").click(function(){
	        		var checked=$(this).attr("checked");
	        		var level=$(this).attr("level");
	        		var name=$(this).attr("name");
	        		var folder=$(this).attr("parent");
	        		var curNaemFirstChar=name.charAt(0);
	        		var conditionName="menuIds";
	        		if("m" == curNaemFirstChar){
	        			conditionName="actionIds";
	        		}else if("a" == curNaemFirstChar){
	        			conditionName="rangeId_";
	        		}
	        		if(1 == folder){
	        			conditionName="menuIds";
	        		}
	        		var childLevel=level+"_";
	        		var operator=$("input[name^="+conditionName+"][level^="+childLevel+"]");
	        		if(checked){
	        			operator.attr("disabled",false);
	        		}else{
	        			$("input[level^="+childLevel+"]").attr("checked",false);
	        			$("input[level^="+childLevel+"]").attr("disabled",true);
	        		}
	        		if(1 != folder){
	        			checkedAllParent(level);
	        		}
	        	});
	        	
	        	var msg='${operatorMsg}';
	        	if(msg) {
	        		$("#operatorSuccess").show();
	        		$("#operatorSuccess").html("<h4>"+msg+"</h4>");
	        		setTimeout("$('#operatorSuccess').hide()", 3000);
	        	};
	        }); 
	        
	        //打开或收缩子部分，v代表父菜单id
	        function openOrClose(v,m){
	        	var open=$(m).attr("openStatus");
	        	if(open == 1){
	        		$("tr[level=childMenu_"+v+"]").hide();
	        		$(m).attr("openStatus",0);
	        		$(m).html("+");
	        	}else{
	        		$("tr[level=childMenu_"+v+"]").show();
	        		$(m).attr("openStatus",1);
	        		$(m).html("—");
	        	}
	        }
	        
	        function checkedAllParent(level){
	        	var parentElement=level.substring(0,level.lastIndexOf("_"));
	        	if(level.split("_").length == 2){
	        		$("input[level="+level+"]").attr("disabled",false).attr("checked",true);
	        	}else{
	        		$("input[level="+parentElement+"]").attr("disabled",false).attr("checked",true);
	        		checkedAllParent(parentElement);
	        	}
	        }
		</script>
	</head>
	<body>
	<div class="container ">
	  <div class="content">
	  	<div id="operatorSuccess"><h4>操作成功</h4></div>
	    <div style="width:90%; height:100%; border:0px solid #ccc; overflow:hidden; clear:both;">
	    	<form action="/rbac/rbac-setting.do" name="rbacForm" method="post">
	    	<input type="hidden" name="roleId" value="${roleId }"/>
	    	<table id="myTable" cellspacing="1" class="tablesorter">
				<thead>
					<tr>
						<th>菜单模块</th>
						<th>操作</th>
						<th>数据范围</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${rbacLayout.menuFolders }" var="menuFolders">
					    <c:set var="checkedThisParentMenu" value="${false }"></c:set>
						<c:forEach items="${roleRbacLayout.roleMenuFolders }" var="roleMenuFolders">
							<c:if test="${!checkedThisParentMenu }">
								<c:if test="${menuFolders.menu.menuId  eq roleMenuFolders.menu.menuId  }">
									<c:set var="checkedThisParentMenu" value="${true }"></c:set>
								</c:if>
							</c:if>
						</c:forEach>
						<tr>
			          		<td class="parentFolder">
			          		    <a href="javascript:;" onclick="openOrClose('${menuFolders.menu.menuId }',this)" openStatus="1">—</a>
				          		<input type="checkbox" name="menuIds" level="m_${menuFolders.menu.menuId }" parent="1" value="${menuFolders.menu.menuId }" ${checkedThisParentMenu ? 'checked=checked' : '' }/>
				          		<span>${menuFolders.menu.menuName }</span>
			          		</td>
			          		<td></td>
			          		<td></td>
			          	</tr>
			          	 <c:forEach items="${menuFolders.childs}" var="childMenu">
			          	 <c:set value="menu_action_${childMenu.menuId }" var="mai"></c:set>
			          	 <c:set value="${rbacLayout[mai] }" var="maiChildAction"></c:set>
			          	 <c:set value="0" var="maiChildActionSize"></c:set>
			          	 <c:if test="${not empty  maiChildAction}">
			          	 	<c:set value="${rbacLayout[mai].size() }" var="maiChildActionSize"></c:set>
			          	 </c:if>
			          	 <c:if test="${maiChildActionSize le 1}">
			          	 	<tr level="childMenu_${menuFolders.menu.menuId }">
			          	 		<c:set var="checkedThisMenu" value="${false }"></c:set>
			          	 		<c:forEach items="${roleRbacLayout.roleMenuFolders }" var="roleMenuFolders">
			          	 			<c:if test="${!checkedThisMenu }">
										<c:forEach items="${roleMenuFolders.childs }" var="roleChildMenu">
											<c:if test="${!checkedThisMenu }">
												<c:if test="${childMenu.menuId  eq roleChildMenu.menuId  }">
													<c:set var="checkedThisMenu" value="${true }"></c:set>
												</c:if>
											</c:if>
										</c:forEach>
									</c:if>
								</c:forEach>
			          			<td class="childMenu"><input type="checkbox" name="menuIds" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }" value="${childMenu.menuId }" ${checkedThisMenu ? 'checked=checked' : '' }/>${childMenu.menuName }</td>
					          	<c:if test="${maiChildActionSize eq 0}">
			          				<td></td><td></td>
			          			</c:if>
					          	<c:if test="${maiChildActionSize eq 1}">
					          		<c:set var="checkedThisAction" value="${false }"></c:set>
					          		<c:forEach items="${roleRbacLayout[mai] }" var="roleMenuAction">
											<c:if test="${!checkedThisAction }">
												<c:if test="${maiChildAction.get(0).actionId  eq roleMenuAction.actionId  }">
													<c:set var="checkedThisAction" value="${true }"></c:set>
												</c:if>
											</c:if>
									</c:forEach>
			          				<td><input type="checkbox" name="actionIds" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }_${maiChildAction.get(0).actionId }" value="${maiChildAction.get(0).actionId }" ${checkedThisAction ? 'checked=checked' : '' }/><span>${maiChildAction.get(0).actionName }</span></td>
			          				<td>
			          					<c:set value="menu_action_range_${childMenu.menuId }_${maiChildAction.get(0).actionId }" var="mari"></c:set>
				          				<c:forEach items="${rbacLayout[mari]}" var="range">
				          					<c:set var="checkedThisRange" value="${false }"></c:set>
				          					<c:forEach items="${roleRbacLayout[mari] }" var="roleActionRange">
													<c:if test="${!checkedThisRange }">
														<c:if test="${range.rangeId  eq roleActionRange.rangeId  }">
															<c:set var="checkedThisRange" value="${true }"></c:set>
														</c:if>
													</c:if>
											</c:forEach>
				          					<input type="${range.isRadio eq 1 ? 'radio' : 'checkbox' }"  name="rangeId_${range.actionId }" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }_${maiChildAction.get(0).actionId }_${range.rangeId }"  value="${range.rangeId }" ${checkedThisRange ? 'checked=checked' : '' }/>${range.rangeName }
				          				</c:forEach>
			          				</td>
			          			</c:if>
			          		</tr>
		          		 </c:if>
		          		 <c:if test="${maiChildActionSize gt 1}">
				          	<tr level="childMenu_${menuFolders.menu.menuId }">
				          		<c:set var="checkedThisMenu" value="${false }"></c:set>
			          	 		<c:forEach items="${roleRbacLayout.roleMenuFolders }" var="roleMenuFolders">
			          	 			<c:if test="${!checkedThisMenu }">
										<c:forEach items="${roleMenuFolders.childs }" var="roleChildMenu">
											<c:if test="${!checkedThisMenu }">
												<c:if test="${childMenu.menuId  eq roleChildMenu.menuId  }">
													<c:set var="checkedThisMenu" value="${true }"></c:set>
												</c:if>
											</c:if>
										</c:forEach>
									</c:if>
								</c:forEach>
				          		<td rowspan="${maiChildActionSize gt 1 ?  maiChildActionSize : 1}" class="childMenu">
				          			<input type="checkbox" name="menuIds" value="${childMenu.menuId }" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }"   ${checkedThisMenu ? 'checked=checked' : '' }/>
				          			<span>${childMenu.menuName }</span>
				          		</td>
				          		<td>
				          			<c:set var="checkedThisAction" value="${false }"></c:set>
					          		<c:forEach items="${roleRbacLayout[mai] }" var="roleMenuAction">
											<c:if test="${!checkedThisAction }">
												<c:if test="${maiChildAction.get(0).actionId  eq roleMenuAction.actionId  }">
													<c:set var="checkedThisAction" value="${true }"></c:set>
												</c:if>
											</c:if>
									</c:forEach>
				          			<span>
				          				<input type="checkbox" name="actionIds" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }_${maiChildAction.get(0).actionId }" value="${maiChildAction.get(0).actionId }" ${checkedThisAction ? 'checked=checked' : '' }/>
				          				<span>${maiChildAction.get(0).actionName }</span>
				          			</span>
				          		</td>
				          		<td>
				          			<span>
					          			<c:set value="menu_action_range_${childMenu.menuId }_${maiChildAction.get(0).actionId }" var="mari"></c:set>
				          				<c:forEach items="${rbacLayout[mari]}" var="range">
						          			<c:set var="checkedThisRange" value="${false }"></c:set>
					          				<c:forEach items="${roleRbacLayout[mari] }" var="roleActionRange">
													<c:if test="${!checkedThisRange }">
														<c:if test="${range.rangeId  eq roleActionRange.rangeId  }">
															<c:set var="checkedThisRange" value="${true }"></c:set>
														</c:if>
													</c:if>
											</c:forEach>
				          					<input type="${range.isRadio eq 1 ? 'radio' : 'checkbox' }" name="rangeId_${range.actionId}" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }_${maiChildAction.get(0).actionId }_${range.rangeId }"  value="${range.rangeId }"  ${checkedThisRange ? 'checked=checked' : '' }/>${range.rangeName }
				          				</c:forEach>
				          			</span>
				          		</td>
				          	</tr>
				          	<c:forEach items="${maiChildAction}" var="action" begin="1">
					          	<tr level="childMenu_${menuFolders.menu.menuId }">
					          		<c:set var="checkedThisAction" value="${false }"></c:set>
					          		<c:forEach items="${roleRbacLayout[mai] }" var="roleMenuAction">
											<c:if test="${!checkedThisAction }">
												<c:if test="${action.actionId  eq roleMenuAction.actionId  }">
													<c:set var="checkedThisAction" value="${true }"></c:set>
												</c:if>
											</c:if>
									</c:forEach>
					          		<td><input type="checkbox" name="actionIds" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }_${action.actionId }" value="${action.actionId }" ${checkedThisAction ? 'checked=checked' : '' }/><span>${action.actionName }</span></td>
					          		<td>
					          			<c:set value="menu_action_range_${childMenu.menuId }_${action.actionId }" var="mari"></c:set>
				          				<c:forEach items="${rbacLayout[mari]}" var="range">
				          					<c:set var="checkedThisRange" value="${false }"></c:set>
					          				<c:forEach items="${roleRbacLayout[mari] }" var="roleActionRange">
													<c:if test="${!checkedThisRange }">
														<c:if test="${range.rangeId  eq roleActionRange.rangeId  }">
															<c:set var="checkedThisRange" value="${true }"></c:set>
														</c:if>
													</c:if>
											</c:forEach>
				          					<input type="${range.isRadio eq 1 ? 'radio' : 'checkbox' }" name="rangeId_${range.actionId}" level="m_${menuFolders.menu.menuId }_${childMenu.menuId }_${action.actionId }_${range.rangeId }"  value="${range.rangeId }" ${checkedThisRange ? 'checked=checked' : '' }/>${range.rangeName }
				          				</c:forEach>
					          		</td>
					          	</tr>
				          	</c:forEach>
				         </c:if>
			          	 </c:forEach>
					</c:forEach>
				</tbody>
			</table>
			<input type="submit" value="提交" class="button blue"/>
			</form>
	    </div>
	 </div>
	</div>
	</body>
</html>